home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxshade4.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.7 KB  |  360 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxshade4.c,v 1.3.2.1 2000/11/29 01:29:50 raph Exp $ */
  20. /* Rendering for Gouraud triangle shadings */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsmatrix.h"        /* for gscoord.h */
  25. #include "gscoord.h"
  26. #include "gxcspace.h"
  27. #include "gxdcolor.h"
  28. #include "gxdevcli.h"
  29. #include "gxistate.h"
  30. #include "gxpath.h"
  31. #include "gxshade.h"
  32. #include "gxshade4.h"
  33.  
  34. /* ---------------- Triangle mesh filling ---------------- */
  35.  
  36. /* Initialize the fill state for triangle shading. */
  37. void
  38. mesh_init_fill_state(mesh_fill_state_t * pfs, const gs_shading_mesh_t * psh,
  39.              const gs_rect * rect, gx_device * dev,
  40.              gs_imager_state * pis)
  41. {
  42.     shade_init_fill_state((shading_fill_state_t *) pfs,
  43.               (const gs_shading_t *)psh, dev, pis);
  44.     pfs->pshm = psh;
  45.     shade_bbox_transform2fixed(rect, pis, &pfs->rect);
  46. }
  47.  
  48. /* Initialize the recursion state for filling one triangle. */
  49. void
  50. mesh_init_fill_triangle(mesh_fill_state_t * pfs,
  51.   const mesh_vertex_t *va, const mesh_vertex_t *vb, const mesh_vertex_t *vc,
  52.   bool check_clipping)
  53. {
  54.     pfs->depth = 1;
  55.     pfs->frames[0].va = *va;
  56.     pfs->frames[0].vb = *vb;
  57.     pfs->frames[0].vc = *vc;
  58.     pfs->frames[0].check_clipping = check_clipping;
  59. }
  60.  
  61. #define SET_MIN_MAX_3(vmin, vmax, a, b, c)\
  62.   if ( a < b ) vmin = a, vmax = b; else vmin = b, vmax = a;\
  63.   if ( c < vmin ) vmin = c; else if ( c > vmax ) vmax = c
  64.  
  65. int
  66. mesh_fill_triangle(mesh_fill_state_t * pfs)
  67. {
  68.     const gs_shading_mesh_t *psh = pfs->pshm;
  69.     gs_imager_state *pis = pfs->pis;
  70.     mesh_frame_t *fp = &pfs->frames[pfs->depth - 1];
  71.     int ci;
  72.  
  73.     for (;;) {
  74.     bool check = fp->check_clipping;
  75.  
  76.     /*
  77.      * Fill the triangle with vertices at fp->va.p, fp->vb.p, and
  78.      * fp->vc.p with color fp->va.cc.  If check is true, check for
  79.      * whether the triangle is entirely inside the rectangle, entirely
  80.      * outside, or partly inside; if check is false, assume the triangle
  81.      * is entirely inside.
  82.      */
  83.     if (check) {
  84.         fixed xmin, ymin, xmax, ymax;
  85.  
  86.         SET_MIN_MAX_3(xmin, xmax, fp->va.p.x, fp->vb.p.x, fp->vc.p.x);
  87.         SET_MIN_MAX_3(ymin, ymax, fp->va.p.y, fp->vb.p.y, fp->vc.p.y);
  88.         if (xmin >= pfs->rect.p.x && xmax <= pfs->rect.q.x &&
  89.         ymin >= pfs->rect.p.y && ymax <= pfs->rect.q.y
  90.         ) {
  91.         /* The triangle is entirely inside the rectangle. */
  92.         check = false;
  93.         } else if (xmin >= pfs->rect.q.x || xmax <= pfs->rect.p.x ||
  94.                ymin >= pfs->rect.q.y || ymax <= pfs->rect.p.y
  95.                ) {
  96.         /* The triangle is entirely outside the rectangle. */
  97.         goto next;
  98.         }
  99.     }
  100.     if (fp < &pfs->frames[countof(pfs->frames) - 3]) {
  101.     /* Check whether the colors fall within the smoothness criterion. */
  102.         for (ci = 0; ci < pfs->num_components; ++ci) {
  103.         float
  104.             c0 = fp->va.cc[ci], c1 = fp->vb.cc[ci], c2 = fp->vc.cc[ci];
  105.         float cmin, cmax;
  106.  
  107.         SET_MIN_MAX_3(cmin, cmax, c0, c1, c2);
  108.         if (cmax - cmin > pfs->cc_max_error[ci])
  109.             goto nofill;
  110.         }
  111.     }
  112.     fill:
  113.     /* Fill the triangle with the color. */
  114.     {
  115.         gx_device_color dev_color;
  116.         const gs_color_space *pcs = psh->params.ColorSpace;
  117.         gs_client_color fcc;
  118.         int code;
  119.  
  120. #if 0
  121.         memcpy(&fcc.paint, fp->va.cc, sizeof(fcc.paint));
  122. #else
  123.         /* Average the colors at the vertices. */
  124.         {
  125.         int ci;
  126.  
  127.         for (ci = 0; ci < pfs->num_components; ++ci)
  128.             fcc.paint.values[ci] =
  129.             (fp->va.cc[ci] + fp->vb.cc[ci] + fp->vc.cc[ci]) / 3.0;
  130.         }
  131. #endif
  132.         (*pcs->type->restrict_color)(&fcc, pcs);
  133.         (*pcs->type->remap_color)(&fcc, pcs, &dev_color, pis,
  134.                       pfs->dev, gs_color_select_texture);
  135.         /****** SHOULD ADD adjust ON ANY OUTSIDE EDGES ******/
  136.         /*
  137.          * See the comment in gx_dc_pattern2_fill_rectangle in gsptype2.c
  138.          * re the choice of path filling vs. direct triangle fill.
  139.          */
  140.         if (pis->fill_adjust.x != 0 || pis->fill_adjust.y != 0) {
  141.         gx_path *ppath = gx_path_alloc(pis->memory, "Gt_fill");
  142.  
  143.         gx_path_add_point(ppath, fp->va.p.x, fp->va.p.y);
  144.         gx_path_add_line(ppath, fp->vb.p.x, fp->vb.p.y);
  145.         gx_path_add_line(ppath, fp->vc.p.x, fp->vc.p.y);
  146.         code = shade_fill_path((const shading_fill_state_t *)pfs,
  147.                        ppath, &dev_color);
  148.         gx_path_free(ppath, "Gt_fill");
  149.         } else {
  150.         code = (*dev_proc(pfs->dev, fill_triangle))
  151.             (pfs->dev, fp->va.p.x, fp->va.p.y,
  152.              fp->vb.p.x - fp->va.p.x, fp->vb.p.y - fp->va.p.y,
  153.              fp->vc.p.x - fp->va.p.x, fp->vc.p.y - fp->va.p.y,
  154.              &dev_color, pis->log_op);
  155.         if (code < 0)
  156.             return code;
  157.         }
  158.     }
  159.     next:
  160.     if (fp == &pfs->frames[0])
  161.         return 0;
  162.     --fp;
  163.     continue;
  164.     nofill:
  165.     /*
  166.      * The colors don't converge.  Does the region color more than
  167.      * a single pixel?
  168.      */
  169.     {
  170.         gs_fixed_rect region;
  171.  
  172.         SET_MIN_MAX_3(region.p.x, region.q.x,
  173.               fp->va.p.x, fp->vb.p.x, fp->vc.p.x);
  174.         SET_MIN_MAX_3(region.p.y, region.q.y,
  175.               fp->va.p.y, fp->vb.p.y, fp->vc.p.y);
  176.         if (region.q.x - region.p.x <= fixed_1 &&
  177.         region.q.y - region.p.y <= fixed_1) {
  178.         /*
  179.          * More precisely, does the bounding box of the region,
  180.          * taking fill adjustment into account, span more than 1
  181.          * pixel center in either X or Y?
  182.          */
  183.         fixed ax = pis->fill_adjust.x;
  184.         int nx =
  185.             fixed2int_pixround(region.q.x + ax) -
  186.             fixed2int_pixround(region.p.x - ax);
  187.         fixed ay = pis->fill_adjust.y;
  188.         int ny =
  189.             fixed2int_pixround(region.q.y + ay) -
  190.             fixed2int_pixround(region.p.y - ay);
  191.  
  192.         if (!(nx > 1 && ny != 0) || (ny > 1 && nx != 0))
  193.             goto fill;
  194.         }
  195.     }
  196.     /*
  197.      * Subdivide the triangle and recur.  The only subdivision method
  198.      * that doesn't seem to create anomalous shapes divides the
  199.      * triangle in 4, using the midpoints of each side.
  200.      *
  201.      * If the original vertices are A, B, C, we fill the sub-triangles
  202.      * in the following order:
  203.      *    (A, AB, AC) - fp[3]
  204.      *    (AB, AC, BC) - fp[2]
  205.      *    (AC, BC, C) - fp[1]
  206.      *    (AB, B, BC) - fp[0]
  207.      */
  208.     {
  209. #define VAB fp[3].vb
  210. #define VAC fp[2].vb
  211. #define VBC fp[1].vb
  212.         int i;
  213.  
  214. #define MIDPOINT_FAST(a,b) arith_rshift_1((a) + (b) + 1)
  215.         VAB.p.x = MIDPOINT_FAST(fp->va.p.x, fp->vb.p.x);
  216.         VAB.p.y = MIDPOINT_FAST(fp->va.p.y, fp->vb.p.y);
  217.         VAC.p.x = MIDPOINT_FAST(fp->va.p.x, fp->vc.p.x);
  218.         VAC.p.y = MIDPOINT_FAST(fp->va.p.y, fp->vc.p.y);
  219.         VBC.p.x = MIDPOINT_FAST(fp->vb.p.x, fp->vc.p.x);
  220.         VBC.p.y = MIDPOINT_FAST(fp->vb.p.y, fp->vc.p.y);
  221. #undef MIDPOINT_FAST
  222.         for (i = 0; i < pfs->num_components; ++i) {
  223.         float ta = fp->va.cc[i], tb = fp->vb.cc[i], tc = fp->vc.cc[i];
  224.  
  225.         VAB.cc[i] = (ta + tb) * 0.5;
  226.         VAC.cc[i] = (ta + tc) * 0.5;
  227.         VBC.cc[i] = (tb + tc) * 0.5;
  228.         }
  229.         /* Fill in the rest of the triangles. */
  230.         fp[3].va = fp->va;
  231.         fp[3].vc = VAC;
  232.         fp[2].va = VAB;
  233.         fp[2].vc = VBC;
  234.         fp[1].va = VAC;
  235.         fp[1].vc = fp->vc;
  236.         fp->va = VAB;
  237.         fp->vc = VBC;
  238.         fp[3].check_clipping = fp[2].check_clipping =
  239.         fp[1].check_clipping = fp->check_clipping = check;
  240. #undef VAB
  241. #undef VAC
  242. #undef VBC
  243.         fp += 3;
  244.     }
  245.     }
  246. }
  247.  
  248. /* ---------------- Gouraud triangle shadings ---------------- */
  249.  
  250. private int
  251. Gt_next_vertex(const gs_shading_mesh_t * psh, shade_coord_stream_t * cs,
  252.            mesh_vertex_t * vertex)
  253. {
  254.     int code = shade_next_vertex(cs, vertex);
  255.  
  256.     if (code >= 0 && psh->params.Function) {
  257.     /* Decode the color with the function. */
  258.     code = gs_function_evaluate(psh->params.Function, vertex->cc,
  259.                     vertex->cc);
  260.     }
  261.     return code;
  262. }
  263.  
  264. inline private int
  265. Gt_fill_triangle(mesh_fill_state_t * pfs, const mesh_vertex_t * va,
  266.          const mesh_vertex_t * vb, const mesh_vertex_t * vc)
  267. {
  268.     mesh_init_fill_triangle(pfs, va, vb, vc, true);
  269.     return mesh_fill_triangle(pfs);
  270. }
  271.  
  272. int
  273. gs_shading_FfGt_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
  274.                    gx_device * dev, gs_imager_state * pis)
  275. {
  276.     const gs_shading_FfGt_t * const psh = (const gs_shading_FfGt_t *)psh0;
  277.     mesh_fill_state_t state;
  278.     shade_coord_stream_t cs;
  279.     int num_bits = psh->params.BitsPerFlag;
  280.     int flag;
  281.     mesh_vertex_t va, vb, vc;
  282.  
  283.     mesh_init_fill_state(&state, (const gs_shading_mesh_t *)psh, rect,
  284.              dev, pis);
  285.     shade_next_init(&cs, (const gs_shading_mesh_params_t *)&psh->params,
  286.             pis);
  287.     while ((flag = shade_next_flag(&cs, num_bits)) >= 0) {
  288.     int code;
  289.  
  290.     switch (flag) {
  291.         default:
  292.         return_error(gs_error_rangecheck);
  293.         case 0:
  294.         if ((code = Gt_next_vertex(state.pshm, &cs, &va)) < 0 ||
  295.             (code = shade_next_flag(&cs, num_bits)) < 0 ||
  296.             (code = Gt_next_vertex(state.pshm, &cs, &vb)) < 0 ||
  297.             (code = shade_next_flag(&cs, num_bits)) < 0
  298.             )
  299.             return code;
  300.         goto v2;
  301.         case 1:
  302.         va = vb;
  303.         case 2:
  304.         vb = vc;
  305. v2:        if ((code = Gt_next_vertex(state.pshm, &cs, &vc)) < 0 ||
  306.             (code = Gt_fill_triangle(&state, &va, &vb, &vc)) < 0
  307.             )
  308.             return code;
  309.     }
  310.     }
  311.     return 0;
  312. }
  313.  
  314. int
  315. gs_shading_LfGt_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
  316.                    gx_device * dev, gs_imager_state * pis)
  317. {
  318.     const gs_shading_LfGt_t * const psh = (const gs_shading_LfGt_t *)psh0;
  319.     mesh_fill_state_t state;
  320.     shade_coord_stream_t cs;
  321.     mesh_vertex_t *vertex;
  322.     mesh_vertex_t next;
  323.     int per_row = psh->params.VerticesPerRow;
  324.     int i, code = 0;
  325.  
  326.     mesh_init_fill_state(&state, (const gs_shading_mesh_t *)psh, rect,
  327.              dev, pis);
  328.     shade_next_init(&cs, (const gs_shading_mesh_params_t *)&psh->params,
  329.             pis);
  330.     vertex = (mesh_vertex_t *)
  331.     gs_alloc_byte_array(pis->memory, per_row, sizeof(*vertex),
  332.                 "gs_shading_LfGt_render");
  333.     if (vertex == 0)
  334.     return_error(gs_error_VMerror);
  335.     for (i = 0; i < per_row; ++i)
  336.     if ((code = Gt_next_vertex(state.pshm, &cs, &vertex[i])) < 0)
  337.         goto out;
  338.     while (!seofp(cs.s)) {
  339.     code = Gt_next_vertex(state.pshm, &cs, &next);
  340.     if (code < 0)
  341.         goto out;
  342.     for (i = 1; i < per_row; ++i) {
  343.         code = Gt_fill_triangle(&state, &vertex[i - 1], &vertex[i], &next);
  344.         if (code < 0)
  345.         goto out;
  346.         vertex[i - 1] = next;
  347.         code = Gt_next_vertex(state.pshm, &cs, &next);
  348.         if (code < 0)
  349.         goto out;
  350.         code = Gt_fill_triangle(&state, &vertex[i], &vertex[i - 1], &next);
  351.         if (code < 0)
  352.         goto out;
  353.     }
  354.     vertex[per_row - 1] = next;
  355.     }
  356. out:
  357.     gs_free_object(pis->memory, vertex, "gs_shading_LfGt_render");
  358.     return code;
  359. }
  360.